import os
import pandas as pd


# 如果运行过程中程序中断,利用check_need检查还未发送成功的邮件

def to_send_list(success_list):
    with open("need_list.txt", 'a+', encoding='utf-8') as f:
        email_str = " ".join(success_list) + "|"
        f.write(email_str)


def check_already_send():
    success_email_list = []
    with open("success_list.txt", 'r', encoding='utf-8') as f:
        success_email_str = f.read()
        if success_email_str:
            all_success_email_list = success_email_str.split("|")
            for success_email_str in all_success_email_list:
                success_email_list = success_email_str.split(" ")
                if success_email_list[0] != "":
                    success_email_list.append(success_email_list)
    Path = os.path.abspath(os.path.dirname(__file__))
    excel_data = pd.read_excel(Path + "/email.xlsx", engine="openpyxl")
    emails = excel_data.iloc[1:].values
    email_list = []
    for email in emails:
        if type(email[4]) == str:
            if '@' in email[4]:
                email_list.append(email[4])

    for i in range(0, len(email_list), 20):
        send_email_list = email_list[i:i + 20]
        if send_email_list not in success_email_list:
            to_send_list(send_email_list)


if __name__ == '__main__':
    check_already_send()
